package com.sws.hmclient.crontab;

import com.sws.hmclient.dao.PidDao;
import com.sws.hmclient.entity.*;
import com.sws.hmclient.util.SystemInfoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.List;

/**
 * @author qushen
 * @date 2021/2/14 18:04
 */
@Component
public class pid {

    @Autowired
    private  PidDao dao;

    @Scheduled(cron = "0 0/1 * * * ? ")   //每1分钟执行一次
    public void pidInfo() throws UnknownHostException, ParseException {
        SystemInfoUtils sysinfo = new SystemInfoUtils();


        //获取系统信息
        Sysinfo record = sysinfo.getSysInfo();
        String computerip = record.getComputerip();


        List<Pid> List=dao.findIpAll();
        for (int i = 0; i < List.size(); i++) {

            if(List.get(i).getIp().equals(computerip)){
                SystemInfoUtils sys=new SystemInfoUtils();
                try {
                    Processesinfo processeByPod = sys.getProcesseByPod(List.get(i).getPid());
                    processeByPod.setIFDELETE(0);
                    processeByPod.setPID(List.get(i).getPid());
                    processeByPod.setIP(List.get(i).getIp());
                    processeByPod.setIFSOCKET(0);
                    dao.insertProcessesinfo(processeByPod);
                }catch (Exception e){
                    Processesinfo processeByPod=new Processesinfo();
                    processeByPod.setIP(computerip);
                    processeByPod.setPID(List.get(i).getPid());
                    processeByPod.setIFSOCKET(1);
                    processeByPod.setIFDELETE(0);
                    dao.insertProcessesinfo(processeByPod);
                }



            }
        }




    }
}
